package com.front.managementfront.customers.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.front.managementfront.utils.CustomPageUtils;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.front.managementfront.customers.service.ICustomersService;
import com.front.managementfront.customers.entity.Customers;

import java.util.List;

/**
 * @author hml
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/front/customers")
public class CustomersController {
    private final Logger logger = LoggerFactory.getLogger(CustomersController.class);

    @Autowired
    public ICustomersService iCustomersService;

    /**
     * 分页查询数据
     *
     * @param page      分页信息
     * @param customers 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getCustomersPageList")
    public Result<Object> getCustomersPageList(Page<Customers> page, Customers customers) {
        IPage<Customers> customersIPage = iCustomersService.selectPage(page, customers);
        List<Customers> records = customersIPage.getRecords();
        customersIPage.setTotal(records.size());
        return ResultUtil.data(CustomPageUtils.getCustomPage(customersIPage));
    }
    /**
     * 返回所有上架数据
     *
     */
    @ResponseBody
    @GetMapping("/getCustomersList")
    public Result<Object> getCustomersList() {
        return ResultUtil.data(iCustomersService.selectList());
    }

    /**
     * 保存和修改公用的
     *
     * @param customers 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/customersSave")
    public Result<Object> customersSave(Customers customers) {
        iCustomersService.saveOrUpdate(customers);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/customersGetById")
    public Result<Object> customersGetById(String id) {
        Customers byId = iCustomersService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/customersDelete")
    public Result<Object> hontUserDelete(String id) {
        iCustomersService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/customersBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iCustomersService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

}
